﻿/// <summary>
/// Developer: CHOOI KAH WAI
/// Last Modified: 21/11/2009
/// </summary>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using ARSS.Editor.Visual;
using System.Xml.Linq;
using ARSS.Editor.Common;
using System.Windows.Media.Media3D;
using ARSS.Common;
using System.Xml;
using System.IO;

namespace ARSS.Editor
{
    /// <summary>
    /// System Core
    /// Controls entire system logic and flow
    /// </summary>
    class Core
    {
        #region Variables

        /// <summary>
        /// Core Singleton
        /// </summary>
        private static Core thisInstance = null;

        #endregion

        #region Properties

        /// <summary>
        /// Get Core Singleton
        /// </summary>
        public static Core Instance
        {
            get
            {
                if (thisInstance == null)
                    thisInstance = new Core();
                return thisInstance;
            }
        }

        /// <summary>
        /// Preview Window Object
        /// </summary>
        public PreviewWindow PreviewWindowObject { get; set; }

        /// <summary>
        /// Scene Nodes Window Object
        /// </summary>
        public SceneNodeWindow SceneNodeWindowObject { get; set; }

        /// <summary>
        /// Animation Window Object
        /// </summary>
        public AnimationWindow AnimationWindowObject { get; set; }

        /// <summary>
        /// Scene Settings Window Object
        /// </summary>
        public SceneSettingsWindow SceneSettingsWindowObject { get; set; }

        /// <summary>
        /// Current scene name
        /// </summary>
        public string SceneName { get; set; }

        /// <summary>
        /// Current scene file path
        /// </summary>
        public string ScenePath { get; set; }

        /// <summary>
        /// Scene Data
        /// </summary>
        public SceneDef SceneData { get; set; }

        #endregion

        #region Functions

        /// <summary>
        /// Constructor
        /// </summary>
        public Core()
        {
            PreviewWindowObject = new PreviewWindow();
            PreviewWindowObject.Show();

            SceneName = "";
            ScenePath = "";

            SceneData = null;

        }

        /// <summary>
        /// Exit application
        /// </summary>
        public void Close()
        {
            if (SceneNodeWindowObject != null)
                SceneNodeWindowObject.Close();
            if (AnimationWindowObject != null)
                AnimationWindowObject.Close();
            if (SceneSettingsWindowObject != null)
                SceneSettingsWindowObject.Close();

            Application.Current.Shutdown();
        }

        /// <summary>
        /// Display Scene Nodes Window
        /// </summary>
        public void ShowSceneNodesWindow()
        {
            if (SceneNodeWindowObject == null)
            {
                SceneNodeWindowObject = new SceneNodeWindow();
                SceneNodeWindowObject.Closing += new System.ComponentModel.CancelEventHandler(WindowObject_Closing);
            }

            SceneNodeWindowObject.Show();
        }

        /// <summary>
        /// Editor window closing event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WindowObject_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (sender == SceneNodeWindowObject)
                SceneNodeWindowObject = null;
            else if (sender == AnimationWindowObject)
                AnimationWindowObject = null;
            else
                SceneSettingsWindowObject = null;
        }

        /// <summary>
        /// Display Animation Window
        /// </summary>
        public void ShowAnimationWindow()
        {
            if (AnimationWindowObject == null)
            {
                AnimationWindowObject = new AnimationWindow();
                AnimationWindowObject.Closing +=new System.ComponentModel.CancelEventHandler(WindowObject_Closing);
            }

            AnimationWindowObject.Show();
        }

        /// <summary>
        /// Display Scene Settings Window
        /// </summary>
        public void ShowSceneSettingsWindow()
        {
            if (SceneSettingsWindowObject == null)
            {
                SceneSettingsWindowObject = new SceneSettingsWindow();
                SceneSettingsWindowObject.Closing += new System.ComponentModel.CancelEventHandler(WindowObject_Closing);
            }

            SceneSettingsWindowObject.Show();
        }

        /// <summary>
        /// Load scene data
        /// </summary>
        public void LoadScene()
        {
            SceneData = new SceneDef();
            FileStream fs = new FileStream(ScenePath, FileMode.Open);
            XmlReader reader = XmlReader.Create(fs);
            SceneData.LoadScene(reader);
            reader.Close();
            fs.Close();

            (PreviewWindowObject as PreviewWindow).ReloadScene();
        }

        #endregion
    }
}
